//
//  QTRegisterViewController.m
//  QTLifeCircle
//
//  Created by 徐腾 on 2017/1/17.
//  Copyright © 2017年 徐腾. All rights reserved.
//

#import "QTRegisterViewController.h"

#import <SMS_SDK/SMSSDK.h>

#import <SVProgressHUD.h>

#import "UserInfo+CoreDataClass.h"
#import "HMCoreDataManager.h"

@interface QTRegisterViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

@property (weak, nonatomic) IBOutlet UITextField *codeTextField;

@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;

@property (weak, nonatomic) IBOutlet UIButton *sendMessageButton;

@end

@implementation QTRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //自定义navigationBar的背景
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"top"] forBarMetrics:UIBarMetricsDefault];
    
    //设置弹框的时间
    [SVProgressHUD setMinimumDismissTimeInterval:0.5];
    
}


//返回登录界面
- (IBAction)backLoginVC:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)sendCode:(id)sender {
    
    if (self.phoneTextField.text.length != 0) {
        
        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneTextField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
            if (error) {
                NSLog(@"%@",error);
                return ;
            }
            [SVProgressHUD showSuccessWithStatus:@"发送短信成功"];
        }];
    } else {
        [SVProgressHUD showErrorWithStatus:@"输入的账号不正确"];
    }
}


- (IBAction)regist:(id)sender {
    
    [SMSSDK commitVerificationCode:self.codeTextField.text phoneNumber:self.phoneTextField.text zone:@"86" result:^(SMSSDKUserInfo *userInfo, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
            return ;
        }
        
        UserInfo *info = [NSEntityDescription insertNewObjectForEntityForName:@"UserInfo" inManagedObjectContext:kHMCoreDataManager.managedObjectContext];
        
        //赋值
        info.phoneNum = self.phoneTextField.text;
        info.password = self.pwdTextField.text;
        
        //保存到数据库
        [kHMCoreDataManager save];
        
        //退出注册界面
        [self backLoginVC:nil];
        
        //弹框
        [SVProgressHUD showSuccessWithStatus:@"注册账号成功"];
        
        NSLog(@"%@",NSHomeDirectory());
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
